package com.zingrow.web.configure.channel.controller;

import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.ExcelUtils;
import com.zingrow.common.utils.OptClass;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.PatternUtil;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.Menu;
import com.zingrow.web.base.enums.Operation;
import com.zingrow.web.common.utils.AnalyzeResult;
import com.zingrow.web.configure.channel.model.Channel;
import com.zingrow.web.configure.channel.service.IChannelService;
import com.zingrow.web.configure.domain.model.Domain;
import com.zingrow.web.configure.domain.service.IDomainService;
import com.zingrow.web.configure.iformation.model.Iformation;
import com.zingrow.web.configure.iformation.service.IIformationService;
import com.zingrow.web.support.operate.model.OptRecord;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.user.model.ActiveUser;
import com.zingrow.web.user.model.UserPermission;
import com.zingrow.web.user.service.IPermissionService;

@Controller
@RequestMapping(value = "/channel/", produces = "application/json;charset=UTF-8")
public class ChannelController extends BaseController {
	@Autowired
	private IChannelService channelService;
	@Autowired
	private IOptRecordService optRecordService;
	@Autowired
	private IIformationService iformationService;
	@Autowired
	private IPermissionService permissionService;
	@Autowired
	private IOptRecordService ioptRecordService;
	@Autowired
	private IDomainService idomainService;

	/*
	 * private static final String url =
	 * "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)"
	 * +
	 * "+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";
	 */

	// 模糊查询
	@ResponseBody
	@RequestMapping("queryByParams")
	public String queryByParams(HttpServletRequest request, HttpServletResponse response, Integer pageIndex,
			Integer pageSize, String channel_url, String channel_name, String channel_type, Integer is_exprot,
			String sp) {
		List<Channel> list = new ArrayList<Channel>();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				list);
		PageView pageView = null;

		try {
			if (null == pageIndex || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (null == pageSize || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			// 字符转换
			if (channel_url != null || "".equals(channel_url)) {
				channel_url = EncodingUtils.fixUrlParamEncoding(request, channel_url);
			}
			if (channel_name != null || "".equals(channel_name)) {
				channel_name = EncodingUtils.fixUrlParamEncoding(request, channel_name);
			}
			if (channel_type != null || "".equals(channel_type)) {
				channel_type = EncodingUtils.fixUrlParamEncoding(request, channel_type);
			}
			list = channelService.queryByParams(pageView, channel_url, channel_name, channel_type, is_exprot);
			if ("1".equals(sp)) {
				// 新增一条操作记录
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().channel_query(opuser, Menu.Resourceallocation.toString(),
						Menu.channel.toString(), Operation.query.toString(), channel_name, channel_url, channel_type);
				if (!s.getOptName().equals("null"))
					ioptRecordService.insert(s);
			}
		} catch (Exception e) {
			log.error("根据参数查询异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置根据参数查询异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(list);
		}
		standardData.setReturnData(list);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		standardData.setTotalPages(pageView.getPageCount());
		standardData.setTotalRecords(pageView.getRowCount());
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping("batchDeletes")
	public String batchDeletes(HttpServletRequest request, HttpServletResponse response) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		// 截获请求的参数，获得要删除的频道ID
		String items = request.getParameter("channel_id");
		List<String> delList = new ArrayList<String>();
		// 用“，”将参数分割存入数组
		String[] strs = items.split(",");
		// 遍历数组将所有要删除的频道ID存入delList集合
		for (String str : strs) {
			delList.add(str);
		}
		if (channelService.batchDeletes(delList)) {
			// 添加一行操作日志
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().goiss(opuser, Menu.Resourceallocation.toString(), Menu.channel.toString(),
					Operation.deldetes.toString(), delList.size());
			ioptRecordService.insert(s);
			// 操作成功
			standardData.setReturnCode(CodeMsg.CODE_OK);
			standardData.setReturnMessage(CodeMsg.MSG_OK);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");

	}

	// 删除
	@ResponseBody
	@RequestMapping("deleteChnnel")

	public String deleteChnnel(HttpServletRequest request, HttpServletResponse response, Integer channel_id,
			String channel_name) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			// 判断参数的合法性
			if (CommonUtils.isObjectNull(channel_id)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			// 字符转换
			if (CommonUtils.isStrNotNull(channel_name)) {
				channel_name = EncodingUtils.fixUrlParamEncoding(request, channel_name);
			}
			if (channelService.deleteChnnel(channel_id)) {
				// 操作记录
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().channel_delete(opuser, Menu.Resourceallocation.toString(),
						Menu.channel.toString(), Operation.delete.toString(), channel_name);
				ioptRecordService.insert(s);
				// 操作成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				// 操作失败
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().recordabnormality(opuser, "频道配置删除失败");
				ioptRecordService.insert(s);
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
			}
		} catch (Exception e) {
			log.error("根据参数删除异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置删除异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 新增
	@ResponseBody
	@RequestMapping("addChannel")
	public String addChannel(HttpServletRequest request, HttpServletResponse response, Channel channle) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			// 判断参数是否正确
			if (CommonUtils.isStrNull(channle.getChannel_name()) || CommonUtils.isStrNull(channle.getChannel_type())
					|| CommonUtils.isStrNull(channle.getChannel_url())
					|| CommonUtils.isObjectNull(channle.getDomainl_id())) {

				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			} else {
				// 转码
				channle.setChannel_name(EncodingUtils.fixUrlParamEncoding(request, channle.getChannel_name()));
				channle.setChannel_type(EncodingUtils.fixUrlParamEncoding(request, channle.getChannel_type()));
				channle.setChannel_url(EncodingUtils.fixUrlParamEncoding(request, channle.getChannel_url()));
			}
			// 判断同名
			if (channelService.queryByName(null, channle.getChannel_url(), channle.getChannel_name())) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
				standardData.setReturnMessage("已存在相同频道名或频道URL，请重新填写");
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			// 验证url格式
			if (!PatternUtil.isUrl(channle.getChannel_url())) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
				standardData.setReturnMessage("URL格式不对,正确格式以https://|http://|ftp://|file://开头");
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			// 调用新增
			if (channelService.addChannel(channle)) {

				// 操作记录
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().channel_add(opuser, Menu.Resourceallocation.toString(),
						Menu.channel.toString(), Operation.insert.toString(), channle);
				ioptRecordService.insert(s);
				// 新增成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().recordabnormality(opuser, "频道配置新增失败");
				ioptRecordService.insert(s);
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
			}
		} catch (Exception e) {
			log.error("新增频道配置异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置新增异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 单个查询
	@ResponseBody
	@RequestMapping("queryById")
	public String queryById(HttpServletRequest request, HttpServletResponse response, Integer channel_id) {
		Channel channel = new Channel();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			// 判断参数是否正确
			if (CommonUtils.isObjectNull(channel_id)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			channel = channelService.queryById(channel_id);
		} catch (Exception e) {
			log.error("根据编号查询异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(channel);
		}
		standardData.setReturnData(channel);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 修改
	@ResponseBody
	@RequestMapping("modifyChannel")
	public String modifyChannel(HttpServletRequest request, HttpServletResponse response, Channel channel) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			// 判断参数是否正确
			if (CommonUtils.isStrNull(channel.getChannel_name()) || CommonUtils.isStrNull(channel.getChannel_type())
					|| CommonUtils.isStrNull(channel.getChannel_url())
					|| CommonUtils.isObjectNull(channel.getDomainl_id())) {

				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			} else {
				// 转码
				channel.setChannel_name(EncodingUtils.fixUrlParamEncoding(request, channel.getChannel_name()));
				channel.setChannel_type(EncodingUtils.fixUrlParamEncoding(request, channel.getChannel_type()));
				channel.setChannel_url(EncodingUtils.fixUrlParamEncoding(request, channel.getChannel_url()));
			}
			// 判断同名
			if (channelService.queryByName(channel.getChannel_id(), channel.getChannel_url(),
					channel.getChannel_name())) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
				standardData.setReturnMessage("已存在相同频道名或频道URL，请重新填写");
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			// 验证url格式
			if (!PatternUtil.isUrl(channel.getChannel_url())) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
				standardData.setReturnMessage("URL格式不对");
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
			Channel ychannel = channelService.queryById(channel.getChannel_id());
			// 调用修改
			if (channelService.modifyChannel(channel)) {
				// 操作成功
				Domain y1 = new Domain();
				Domain y2 = new Domain();
				if (!ychannel.getDomainl_id().equals(channel.getDomainl_id())) {
					y1 = idomainService.queryByid(ychannel.getDomainl_id());
					y2 = idomainService.queryByid(channel.getDomainl_id());
				}
				ActiveUser opuser = getUser(request);// 获取操作用户
				OptRecord s = new OptClass().channel_updata(opuser, Menu.Resourceallocation.toString(),
						Menu.channel.toString(), Operation.update.toString(), ychannel, channel, y1, y2);
				if (!s.getOptName().equals("null"))
					ioptRecordService.insert(s);
				// 修改成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
			}
		} catch (Exception e) {
			log.error("修改频道异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置修改频道异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
			standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 查询全部的接口
	@ResponseBody
	@RequestMapping("queryByIdName")
	public String queryByIdName(HttpServletRequest request, HttpServletResponse response, Integer pageIndex,
			Integer pageSize) {
		List<Channel> list = new ArrayList<Channel>();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				list);
		PageView pageView = null;
		try {
			if (pageIndex == null || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (pageSize == null || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			list = channelService.queryByIdName(pageView);
		} catch (Exception e) {
			log.error("查询异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(list);
		}
		standardData.setReturnData(list);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		standardData.setTotalPages(pageView.getPageCount());
		standardData.setTotalRecords(pageView.getRowCount());
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 频道模板
	@ResponseBody
	@RequestMapping("exportExcelForModel")
	public String exportExcelForModel(HttpServletRequest request, HttpServletResponse response) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		List<Channel> list = new ArrayList<Channel>();
		try {
			List<String> titles = Arrays.asList("频道url", "频道类型", "频道名称");
			List<String> paramsName = Arrays.asList("channel_url", "channel_type", "channel_name");
			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 定义输出文件头
				response.setHeader("Content-disposition",
						"attachment; filename=" + new String("频道配置模版".getBytes("GBK"), "iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titles, paramsName, list, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().modelxz(opuser, Menu.Resourceallocation.toString(), Menu.channel.toString(),
					Operation.modelxz.toString(), "导入模板下载");
			ioptRecordService.insert(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 导出
	@ResponseBody
	@RequestMapping("exportExcelForPageData")
	public String exportExcelForPageData(HttpServletRequest request, HttpServletResponse response, String channel_id) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			// 定义表头
			List<String> titls = Arrays.asList("编号", "站点", "标签值", "频道url地址", "频道类型", "频道名称", "频道状态");
			// 定义表的字段名
			List<String> paramsName = Arrays.asList("channel_id", "domain_name", "tagvalue_id", "channel_url",
					"channel_type", "channel_name", "is_deletes");
			// 处理传入的id，并查询出列表
			List<Integer> idlist = new ArrayList<>();
			List<Channel> channel = null;

			if (StringUtils.isNoneBlank(channel_id)) {
				// 分割id
				for (String strids : channel_id.split(",")) {
					idlist.add(Integer.valueOf(strids));
				}
				channel = channelService.queryChannelbyid(idlist);
			}

			// 定义一个输出流
			OutputStream out = null;
			try {
				// 获取输出流
				out = response.getOutputStream();
				// 清空流
				response.reset();
				// 定义响应输出类型
				response.setContentType("application/msexcel");
				// 设置输出文件头
				response.setHeader("Content-disposition",
						"attachment; filename=" + new String("频道信息".getBytes("GBK"), "iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titls, paramsName, channel, out);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				// 关闭流
				out.close();
			}
			// 新增一条操作记录
			// 操作日志
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().goiss(opuser, Menu.Resourceallocation.toString(), Menu.channel.toString(),
					Operation.daochu.toString(), idlist.size());
			ioptRecordService.insert(s);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 导入
	@ResponseBody
	@RequestMapping("importExcelModel")
	public String importExcelModel(HttpServletRequest request, HttpServletResponse response) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		// 获取Web项目的全路径
		String realPath = request.getSession().getServletContext().getRealPath("/uploadTemp/");
		File saveFile = null;
		// 如果目录不存在就创建
		if (!new File(realPath).exists()) {
			new File(realPath).mkdir();
		}
		saveFile = new File(new File(realPath),
				getUser(request).getUserAccount() + System.currentTimeMillis() + "channel_temp.xls");
		// 把request转型
		DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;
		// 获得文件
		MultipartFile multipartFile = multipartRequest.getFile("importExcel");
		// 保存解析出来的信息
		AnalyzeResult<Channel> analyzeResult = new AnalyzeResult<Channel>(true, "", new ArrayList<Channel>());
		// 接收导入失败的数据，成功此list的size为0，否则里面的数据为错误数据
		List<Integer> errorIndexList = new ArrayList<Integer>();
		try {
			// 转换成file
			multipartFile.transferTo(saveFile);
			// 进行数据检测,将数据写入analyzeResult.list中
			channelService.analyzeExcelData(saveFile, analyzeResult);
			if (analyzeResult.isSuccess()) {
				List<Channel> list = analyzeResult.getList();
				// 循环判断是否重复
				for (Channel channel : list) {
					if (channelService.queryByName(null, channel.getChannel_url(), channel.getChannel_name())) {
						standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
						standardData.setReturnMessage("Excel中频道URL或频道名称有重复值");
						return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
					}
				}
				// 通过判断重复值，执行新增
				for (Channel channel : list) {
					channelService.addChannel(channel);
				}
				if (errorIndexList.size() == 0) {
					// 新增一条操作记录
					// 操作日志
					ActiveUser opuser = getUser(request);// 获取操作用户
					OptRecord s = new OptClass().goiss(opuser, Menu.Resourceallocation.toString(),
							Menu.channel.toString(), Operation.daoru.toString(), list.size());
					ioptRecordService.insert(s);
					// 导入数据成功
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage(CodeMsg.MSG_OK);
				} else {
					// 有导入错误返回错误行数
					ActiveUser opuser = getUser(request);// 获取操作用户
					OptRecord s = new OptClass().goiss(opuser, Menu.Resourceallocation.toString(),
							Menu.channel.toString(), Operation.daoru.toString(), list.size() - errorIndexList.size());
					ioptRecordService.insert(s);
					standardData.setReturnCode(CodeMsg.CODE_ERROR_SAME);
					standardData.setReturnMessage("导入数据完成，存在部分数据导入失败：" + errorIndexList.size());
				}
			} else {
				// 检测数据失败，返回相应提示信息
				standardData.setReturnCode(CodeMsg.CODE_ERROR_OTHER);
				standardData.setReturnMessage("检测数据格式错误：" + analyzeResult.getErrorMessage());
			}
		} catch (Exception e) {
			log.error("导入频道信息异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置导入频道信息异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_ERROR);
			standardData.setReturnMessage(CodeMsg.MSG_ERROR);
		} finally {
			// 结束后删除本地文件
			saveFile.delete();
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	// 新方案通过类别查询频道名称
	@ResponseBody
	@RequestMapping("queryChannelByType")
	public String queryByParams(HttpServletRequest request, HttpServletResponse response, Integer pageIndex,
			Integer pageSize, String channel_name, String channel_type, String ifm_id, String types,
			String channel_url) {
		List<Channel> list = new ArrayList<Channel>();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				list);
		PageView pageView = null;
		try {
			List<String> k_1 = new ArrayList<String>();
			String[] s_1 = null;
			if (!"".equals(ifm_id) && ifm_id != null) {
				int s = Integer.parseInt(ifm_id);
				Iformation k = iformationService.selectbyifmid(s);
				if (channel_type.equals("News")) {
					if (types.equals("p_newsType")) {
						s_1 = k.getIfm_getChannel().split(",");
					} else {
						s_1 = k.getIfm_notChannel().split(",");
					}
				} else if (channel_type.equals("BBS")) {
					if (types.equals("p_bbsType")) {
						s_1 = k.getIfm_getBbsName().split(",");
					} else {
						s_1 = k.getIfm_notBbsName().split(",");
					}

				}
				if (s_1.length > 0) {
					for (int i = 0; i < s_1.length; i++)
						k_1.add(s_1[i]);
					// 修改频道显示优先级
					if (channelService.modifyDisplay(1, k_1)) {
					}
				}
			}
			if (pageIndex == null || pageIndex <= 0) {
				pageIndex = 1;
			}
			if (pageSize == null || pageSize <= 0) {
				pageSize = 10;
			}
			pageView = new PageView(pageSize, pageIndex);
			// 字符转换
			if (channel_name != null || "".equals(channel_name)) {
				channel_name = EncodingUtils.fixUrlParamEncoding(request, channel_name);
				System.out.println(channel_name);
			}
			if (channel_type != null || "".equals(channel_type)) {
				channel_type = EncodingUtils.fixUrlParamEncoding(request, channel_type);
			}
			list = channelService.queryChannelByType(pageView, channel_type, channel_name);
			// 修改频道显示优先级
			if (!"".equals(ifm_id) && s_1.length > 0 && ifm_id != null) {
				if (channelService.modifyDisplay(0, k_1)) {
				}
				;
			}
		} catch (Exception e) {
			log.error("根据参数查询异常");
			ActiveUser opuser = getUser(request);// 获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser, "频道配置查询异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			standardData.setReturnData(list);
		}

		standardData.setReturnData(list);
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		standardData.setTotalPages(pageView.getPageCount());
		standardData.setTotalRecords(pageView.getRowCount());
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
}
